﻿using Client;
using Shared.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ManageSystem.Dialog
{
    public partial class DialogLine : Form
    {
        private TextBox _lineIdText = null;
        private TextBox _flowIdText = null;
        private LINE _lineW = null;
        private List<LINE> _listLine = null;

        public DialogLine(TextBox lineId,TextBox flowId=null,LINE lineW=null)
        {
            _lineIdText = lineId;
            _flowIdText = flowId;
            _lineW = lineW;
            InitializeComponent();
        }

        //设置listview
        private void SetListView(ListView view, LINE line)
        {
            if (view == null || line == null) return;
            ListViewItem item = new ListViewItem();
            item.Text = line.ID.ToString();

            item.SubItems.Add(line.NAME);

            if (line.FLOW_ID != null)
                item.SubItems.Add(line.FLOW_ID.ToString());
            else
                item.SubItems.Add("");

            item.SubItems.Add(line.CONTEXT);

            if (line.LEADER_ID != null)
                item.SubItems.Add(line.LEADER_ID.ToString());
            else
                item.SubItems.Add("");

            item.SubItems.Add(line.LEADER_NAME);

            view.Items.Add(item);
        }
        //清空list view
        private void ClearListView(ListView view)
        {
            view.Items.Clear();
        }
        //加载产线信息
        private void DialogLine_Load(object sender, EventArgs e)
        {
            ClearListView(this.listViewLine);

            if (_lineW == null) _lineW = new LINE();
            var selectLine = MethodWcf.SelectLineClient(_lineW);
            if (selectLine.result)
            {
                _listLine = selectLine.value;
                for(int i = 0; i < _listLine.Count; i++)
                {
                    SetListView(this.listViewLine, _listLine[i]);
                }
            }
        }
        //选择产线
        private void listViewLine_MouseClick(object sender, MouseEventArgs e)
        {
            var selectLineId = long.Parse(this.listViewLine.SelectedItems[0].Text);
            var lineRow = (from l in _listLine
                           where l.ID == selectLineId
                           select l).FirstOrDefault();

            if (lineRow == null)
            {
                MessageBox.Show("未能找到选择的产线");
                Close();
                return;
            }

            _lineIdText.Text = lineRow.ID.ToString();
            if (lineRow.FLOW_ID != null&&_flowIdText!=null)
                _flowIdText.Text = lineRow.FLOW_ID.ToString();

            Close();
        }
    }
}
